#include "Timestamp.h"

Timestamp::Timestamp(int64_t microSecondsSinceEpoch)
    : microSecondsSinceEpoch_(microSecondsSinceEpoch)
{
}

Timestamp Timestamp::now()
{
    return Timestamp(time(nullptr));
}

std::string Timestamp::toString() const
{
    char buf[128] = {0};
    struct tm *tm_time = localtime(&microSecondsSinceEpoch_);
    snprintf(buf, sizeof(buf), "%04d-%02d-%02d %02d:%02d:%02d",
             tm_time->tm_year + 1900, // 年份从1900年开始计数
             tm_time->tm_mon + 1,     // 月份从0开始计数，需要加1
             tm_time->tm_mday,        // 日期
             tm_time->tm_hour,        // 小时
             tm_time->tm_min,         // 分钟
             tm_time->tm_sec);        // 秒

    return buf;
}

